﻿Imports System.IO

Module Module1
#Region "Keywords"
    Dim Comment As Int32 = 1
    Dim Push As Int32 = 2
    Dim PushString As Int32 = 3
    Dim Print As Int32 = 4
    Dim Halt As Int32 = 5
    Dim WaitForKey As Int32 = 6
    Dim ClearScreen As Int32 = 7
    Dim WaitTime As Int32 = 8
    Dim Show As Int32 = 9
#End Region


    Sub Main()
        Dim args As String() = Environment.GetCommandLineArgs()
        If args.Length = 1 Then
        ElseIf args.Length = 2 Then
            If File.Exists(args(1)) Then
                If args(1).EndsWith(".fsc") Then
                    Compile(args(1))
                End If
            Else
                Console.WriteLine("File doesn't exist!")
                'args(1) = "C:\test.ir32"
                'Main()
            End If
        ElseIf args.Length = 3 Then
            If args.Contains("-exe") Then
                For Each arg In args
                    If Not arg = "fsc" Then
                        If File.Exists(arg) Then
                            If arg.EndsWith(".fsc") Then
                                'Compile(arg)
                                'File.Create("abc.vb")
                                File.WriteAllText("abc.vb", CodeConverter.FStoVB(File.ReadAllText(arg), "abc"))
                            End If
                        End If
                    End If
                Next
            End If
        ElseIf args.Length = 123 Then
            For Each arg In args
                If Not arg = "fsc" Then
                    If arg = "-exe" Then

                    End If
                End If
            Next
            Dim exe = False
            For i = 0 To args.Length - 1
                If args(i) = "-exe" Then
                    exe = True

                End If
            Next
        End If



 
    End Sub


    Public Sub Compile(abc As String)
        Dim code As String() = File.ReadAllLines(abc)
        Dim result As String
        Dim wr As New BinaryWriter(New FileStream(abc.Replace("fsc", "") + ".fsx", FileMode.Create))
        wr.Write("FLSCRIPT")
        For Each line In code
            line = line.ToUpper
            If line.StartsWith("##") Then
                wr.Write(Comment)
            ElseIf line.StartsWith("PUSH ") Then
                wr.Write(Push)
                If Int32.TryParse(line.Substring(5), New Int32) Then
                    wr.Write(line.Substring(5))
                Else
                    Console.BackgroundColor = ConsoleColor.Red
                    Console.WriteLine("Syntax error : trying to push a non-integer object")
                End If
            ElseIf line.StartsWith("WAIT ") Then
                wr.Write(WaitTime)
                wr.Write(line.Substring(5))
            ElseIf line.StartsWith("SHOW ") Then
                wr.Write(Show)
                wr.Write(line.Substring(5))
            ElseIf line.StartsWith("PUSH$ ") Then
                wr.Write(PushString)
                wr.Write(line.Substring(6))
            ElseIf line = "PRT" Then
                wr.Write(Print)
            ElseIf line = "HLT" Then
                wr.Write(Halt)
            ElseIf line = "WFK" Then
                wr.Write(WaitForKey)
            ElseIf line = "CLS" Then
                wr.Write(ClearScreen)
            End If
        Next
    End Sub

End Module
